package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.trkj.dao.jpa.IjlCaseDAO;
import com.trkj.dao.jpa.IjlElectronicePrintDAO;
import com.trkj.dao.jpa.IjlJlCheckResultDAO;
import com.trkj.dao.jpa.IjliccardDAO;
import com.trkj.pojos.JlCase;
import com.trkj.pojos.JlCheckResult;
import com.trkj.pojos.JlElectronicePrint;
import com.trkj.pojos.JlIccard;
import com.trkj.vo.JlIccardAndEleVo;

@Service
@Transactional
public class JlCaseService {
	@Autowired
	private IjlCaseDAO CaseDAO;
	@Autowired
	private IjliccardDAO iccardDAO;
	@Autowired
	private IjlElectronicePrintDAO ElectronicePrintDAO;
	@Autowired
	private IjlJlCheckResultDAO CheckResultDAO;
	 /*
		 * 查看病例信息
		 */
		public Map<String, Object> queryreginByPage(int page, int rows,Long id){			
				PageRequest pr = new PageRequest(page - 1, rows);
				//获取当前页的记录
				BigDecimal needid=new BigDecimal(id);//根据卡号查找查找电子处方信息
				List<JlIccardAndEleVo> list= iccardPotocaseVoList(ElectronicePrintDAO.findElectronicePrintByPage(pr,needid),id);					
				//获取总的记录条数
				long total = ElectronicePrintDAO.count();			
				Map<String, Object> map = new HashMap<>();
				map.put("total", total);
				map.put("rows", list);
				return map;
		}
		//根据条件查找
		public List<JlCase> findreginBy(long sex){
			List<JlCase> list=CaseDAO.findbyreks(sex);
				return list;
		}  
		//卡号po转casevo  
		private List<JlIccardAndEleVo> iccardPotocaseVoList(List<JlElectronicePrint> list,Long id){
			if(list == null) return null;
			List<JlIccardAndEleVo> listVo = new ArrayList<>();
			for(JlElectronicePrint stu: list){
				BigDecimal iid=new BigDecimal(id);//根据卡号查找检查信息
				List<JlCheckResult> list3=CheckResultDAO.findBycrId(iid);
				if( list3.size()<=0){
					JlIccardAndEleVo stuVo = new JlIccardAndEleVo();
					stuVo.setErId(stu.getErId());
					stuVo.setAdviceId(stu.getAdviceId());
					stuVo.setCheckNy(stu.getCheckNy());
					stuVo.setErOpenTime(stu.getErOpenTime());
					stuVo.setErPhysician(stu.getErPhysician());
					stuVo.setIerId(stu.getIerId());
					stuVo.setInpectionResult(stu.getInpectionResult());
					stuVo.setRerId(stu.getRerId());
					stuVo.setState(stu.getState());
					JlIccard iccard=iccardDAO.findOne(id);
					stuVo.setiId(iccard.getIId());
					stuVo.setiAddress(iccard.getIAddress());
					stuVo.setiAge(iccard.getIAge());
					stuVo.setiIdentityCard(iccard.getIIdentityCard());
					stuVo.setiName(iccard.getIName());
					stuVo.setiOpenTime(iccard.getIOpenTime());
					stuVo.setiPho(iccard.getIPho());
					stuVo.setiSex(iccard.getISex());
					listVo.add(stuVo);
				}else{
						for(JlCheckResult stu3: list3){
								JlIccardAndEleVo stuVo = new JlIccardAndEleVo();
								stuVo.setErId(stu.getErId());
								stuVo.setAdviceId(stu.getAdviceId());
								stuVo.setCheckNy(stu.getCheckNy());
								stuVo.setErOpenTime(stu.getErOpenTime());
								stuVo.setErPhysician(stu.getErPhysician());
								stuVo.setIerId(stu.getIerId());
								stuVo.setInpectionResult(stu.getInpectionResult());
								stuVo.setRerId(stu.getRerId());
								stuVo.setState(stu.getState());
								stuVo.setCrCheckResult(stu3.getCrCheckResult());
								stuVo.setCrRemark(stu3.getCrRemark());
								stuVo.setCrCheckDate(stu3.getCrCheckDate());
								stuVo.setCrFigurePerson(stu3.getCrFigurePerson());
								stuVo.setCrId(stu3.getCrId());
								stuVo.setIcrId(stu3.getIcrId());							
								JlIccard iccard=iccardDAO.findOne(id);								
								stuVo.setiId(iccard.getIId());
								stuVo.setiAddress(iccard.getIAddress());
								stuVo.setiAge(iccard.getIAge());
								stuVo.setiIdentityCard(iccard.getIIdentityCard());
								stuVo.setiName(iccard.getIName());
								stuVo.setiOpenTime(iccard.getIOpenTime());
								stuVo.setiPho(iccard.getIPho());
								stuVo.setiSex(iccard.getISex());	
								listVo.add(stuVo);
							}
							
						}				
			}			
				return listVo;				
		}				
}
